package com.danger.Activity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.danger.Model.FileInfo;
import com.danger.Service.DownloadService;
import com.danger.Widget.HorizontalProgressBarWithProgress;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private TextView name_textView;
    private static HorizontalProgressBarWithProgress horizontal_progressbar;
    private Button download_button;
    private Button stop_button;
    private FileInfo fileInfo;
    private boolean isStop = true;
    public static Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1:
                    horizontal_progressbar.setProgress((int) msg.obj);
                    break;
                default:
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //注册广播接收器
//        IntentFilter filter = new IntentFilter();
//        filter.addAction(DownloadService.ACTION_UPDATE);
//        registerReceiver(receiver, filter);

        initView();

        initEvent();
    }

    private void initView() {
        name_textView = (TextView) findViewById(R.id.name_textView);
        horizontal_progressbar = (HorizontalProgressBarWithProgress) findViewById(R.id.horizontal_progressbar);
        download_button = (Button) findViewById(R.id.download_button);
        stop_button = (Button) findViewById(R.id.stop_button);
        stop_button.setClickable(false);
        horizontal_progressbar.setMax(100);
        fileInfo = new FileInfo(0, "http://c5.97you.net/download/颜色拾取器_11@27905.exe", "颜色拾取器", 0, 0);
        name_textView.setText("颜色拾取器");
    }

    private void initEvent() {
        download_button.setOnClickListener(this);
        stop_button.setOnClickListener(this);
    }

    /**
     * 通过Intent传递参数给Service
     *
     * @param v
     */
    @Override
    public void onClick(View v) {
        Intent intent = new Intent(MainActivity.this, DownloadService.class);
        switch (v.getId()) {
            case R.id.download_button:
                download_button.setClickable(false);
                stop_button.setClickable(true);
                intent.setAction(DownloadService.ACTION_START);
                break;
            case R.id.stop_button:
                download_button.setClickable(true);
                stop_button.setClickable(false);
                intent.setAction(DownloadService.ACTION_STOP);
                break;
            default:
                break;
        }
        intent.putExtra("fileInfo", fileInfo);
        startService(intent);
    }

    /**
     * 更新UI的广播接收器
     */
    BroadcastReceiver receiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (DownloadService.ACTION_UPDATE.equals(intent.getAction())) {
                int finished = intent.getIntExtra("finished", 0);
                horizontal_progressbar.setProgress(finished);
            }
        }
    };

    @Override
    protected void onDestroy() {
        super.onDestroy();
//        unregisterReceiver(receiver);
    }
}
